import 'package:flutter/material.dart';
import '../app/app_setting.dart';
import '../l10n/app_localizations.dart';
import 'app/app_services.dart';


class SettingsPage extends StatelessWidget {
  const SettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    // 获取 AppSettings 实例，用于读取当前状态和调用更新方法
    final settings = AppServices.appSettings;
    // 获取国际化文本
    final local = AppLocalizations.of(context)!;

    return Scaffold(
      appBar: AppBar(
        title: Text(local.settings),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          // 语言设置
          _buildSectionHeader(context, local.language),
          _buildLanguageDropdown(context, settings, local),
          const SizedBox(height: 24),
          // 主题设置
          _buildSectionHeader(context, local.theme),
          _buildThemeDropdown(context, settings, local),
        ],
      ),
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8.0),
      child: Text(
        title,
        style: Theme.of(context).textTheme.titleLarge?.copyWith(
          color: Theme.of(context).colorScheme.primary,
        ),
      ),
    );
  }

  // 语言选择下拉菜单
  Widget _buildLanguageDropdown(BuildContext context, AppSettings settings, AppLocalizations local) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0),
        child: DropdownButton<Locale>(
          value: settings.locale,
          isExpanded: true,
          underline: const SizedBox.shrink(), // 隐藏下划线
          onChanged: (Locale? newLocale) {
            if (newLocale != null) {
              settings.updateLocale(newLocale);
            }
          },
          items: const [
            DropdownMenuItem(
              value: Locale('zh'),
              child: Text('中文'),
            ),
            DropdownMenuItem(
              value: Locale('en'),
              child: Text('English'),
            ),
          ],
        ),
      ),
    );
  }

  // 主题选择下拉菜单
  Widget _buildThemeDropdown(BuildContext context, AppSettings settings, AppLocalizations local) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0),
        child: DropdownButton<ThemeMode>(
          value: settings.themeMode,
          isExpanded: true,
          underline: const SizedBox.shrink(),
          onChanged: (ThemeMode? newMode) {
            if (newMode != null) {
              settings.updateThemeMode(newMode);
            }
          },
          items: [
            DropdownMenuItem(
              value: ThemeMode.system,
              child: Text(local.themeSystem),
            ),
            DropdownMenuItem(
              value: ThemeMode.light,
              child: Text(local.themeLight),
            ),
            DropdownMenuItem(
              value: ThemeMode.dark,
              child: Text(local.themeDark),
            ),
          ],
        ),
      ),
    );
  }
}
